//
//  XSVistorView.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/3.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit
//import SnapKit

@objc protocol XSVistorViewDelegate:NSObjectProtocol{
    
    optional func vistorView(vistorView:XSVistorView,loginBtnDidClick:UIButton)
}



class XSVistorView: UIView {

    weak var delegate:XSVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        startAnim()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置页面信息
    func setPageInformation(imageName:String?,infor:String?){
        if imageName == nil{
            startAnim()
        }else{
            rotateView.hidden = true
            descLabel.text = infor
            iconView.image = UIImage(named: imageName!)
        }
        
    }
    
    //懒加载控件 小房子
    private lazy var iconView:UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_house")
        let imageView = UIImageView(image: image)
        
        return imageView
    }()
    
    //控件转动的圆
    private lazy var rotateView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //空间label
    private lazy var descLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: 16)
        
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.text = "玲珑骰子安红豆,入骨相思知不知."
        return label
    }()
    //注册按钮
    private lazy var registerButton:UIButton = {
        let btn = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        btn.addTarget(self, action: "btnDidClick:", forControlEvents: .TouchUpInside)
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:.Normal)
        return btn
    }()
    //登录按钮
    private lazy var loginButton:UIButton = {
        let btn = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        btn.addTarget(self, action: "btnDidClick:", forControlEvents: .TouchUpInside)
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:.Normal)
        return btn
    }()
    
    //
    func btnDidClick(btn:UIButton){
        delegate?.vistorView?(self, loginBtnDidClick: btn)
    }

    
    //遮罩层
    private lazy var maskLayerView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //圆圈转动动画
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //转动角度
        anim.toValue = 2 * M_PI
        //执行时间
        anim.duration = 30
        //重复次数
        anim.repeatCount = MAXFLOAT
        //程序进入后台或者,或者切换tabBar后,动画会释放
        anim.removedOnCompletion = false
        
        rotateView.layer.addAnimation(anim, forKey: nil)
    }
    
    func setupUI(){
        self.backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        //添加子控件
        addSubview(descLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        addSubview(rotateView)
        addSubview(maskLayerView)
        addSubview(iconView)
        
        //1.icon添加约束
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        //2.旋转圆圈添加约束
//        rotateView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: rotateView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: rotateView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        rotateView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        //3.label添加约束
//        descLabel.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: descLabel, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: rotateView, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: descLabel, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 122))
//        
//        addConstraint(NSLayoutConstraint(item: descLabel, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: rotateView, attribute: .Bottom, multiplier: 1, constant: 0))
        descLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(rotateView.snp_centerX)
            make.width.equalTo(122)
            make.top.equalTo(rotateView.snp_bottom)
        }
        
        //4.注册按钮添加约束
//        registerButton.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: .Leading, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: descLabel, attribute: .Bottom, multiplier: 1, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 50))
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(descLabel.snp_leading)
            make.top.equalTo(descLabel.snp_bottom).offset(16)
            make.width.equalTo(50)
        }
        
        //5.登录按钮添加约束
//        loginButton.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: .Trailing, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 50))
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(descLabel.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(50)
        }
        //6.遮罩层
//        maskLayerView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: maskLayerView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: maskLayerView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: -15))
        maskLayerView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView).offset(-10)
        }
    }
    
    
}
